package com.sskj.fabi.ui.activity;


import android.support.v4.content.ContextCompat;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;

import com.alibaba.android.arouter.facade.annotation.Route;
import com.alibaba.android.arouter.launcher.ARouter;
import com.bulong.rudeness.RudenessScreenHelper;
import com.shizhefei.mvc.MVCCoolHelper;
import com.shizhefei.view.coolrefreshview.CoolRefreshView;
import com.sskj.common.adapter.ItemClickSupport;
import com.sskj.common.adapter.slimadapter.IViewInjector;
import com.sskj.common.adapter.slimadapter.SlimAdapter;
import com.sskj.common.adapter.slimadapter.SlimInjector;
import com.sskj.common.base.App;
import com.sskj.common.box.decoration.DividerLineItemDecoration;
import com.sskj.fabi.R;
import com.sskj.fabi.R2;
import com.sskj.fabi.bean.FabiOrderStatusEnum;
import com.sskj.fabi.bean.OrderRecordBean;
import com.sskj.fabi.component.DaggerUserDataComponent;
import com.sskj.fabi.presenter.OrderRecordActivityPresenter;
import com.sskj.lib.Constans;
import com.sskj.lib.RConfig;
import com.sskj.lib.base.BaseActivity;
import com.sskj.lib.bean.FabiBuySellEnum;
import com.sskj.lib.bean.UserData;
import com.sskj.lib.model.room.UserViewModel;
import com.sskj.lib.mvchelper.ModelRx2DataSource;
import com.sskj.lib.util.CoinUtil;
import com.sskj.lib.util.StringUtil;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import javax.inject.Inject;

import butterknife.BindView;

@Route(path = RConfig.FABI_ORDER_RECORD)
public class OrderRecordActivity extends BaseActivity<OrderRecordActivityPresenter> {
    @BindView(R2.id.recyclerView)
    RecyclerView recyclerView;
    @BindView(R2.id.coolRefreshView)
    CoolRefreshView coolRefreshView;
    private MVCCoolHelper<List<OrderRecordBean>> mvcHelper;
    @Inject
    UserViewModel userViewModel;
    private UserData userData;
    private SlimAdapter slimAdapter;

    @Override
    protected void onResume() {
        if (mvcHelper != null) {
            mvcHelper.refresh();
        }
        super.onResume();
    }

    @Override
    protected int getLayoutId() {
        return R.layout.fabi_activity_order_record;
    }

    @Override
    public OrderRecordActivityPresenter getPresenter() {
        return new OrderRecordActivityPresenter();
    }

    @Override
    protected void initView() {
        ARouter.getInstance().inject(this);
        setTitle(App.INSTANCE.getString(R.string.fabi_publishRecordActivity1_j));

        DaggerUserDataComponent.create().inject(this);
        userViewModel.getUsers().observe(this, users -> {
            if (users != null && users.size() > 0) {
                userData = users.get(0);
            } else {
                userData = null;
            }
        });
        HashMap<String, String> statusMap = new HashMap<>();
        statusMap.put("1", FabiOrderStatusEnum.待付款.getTitle());
        statusMap.put("2", FabiOrderStatusEnum.待放币.getTitle());
        statusMap.put("3", FabiOrderStatusEnum.已完成.getTitle());
        statusMap.put("4", FabiOrderStatusEnum.申诉中.getTitle());
        statusMap.put("5", FabiOrderStatusEnum.已取消.getTitle());
        recyclerView.setLayoutManager(new LinearLayoutManager(this));
        recyclerView.addItemDecoration(new DividerLineItemDecoration(this)
                .setFirstDraw(true)
                .setPaintWidth((int) RudenessScreenHelper.pt2px(this, 20))
                .setDividerColor(ContextCompat.getColor(App.INSTANCE, R.color.transparent)));
        slimAdapter = SlimAdapter.create().register(R.layout.fabi_recy_item_order_record, new SlimInjector<OrderRecordBean>() {

            @Override
            public void onInject(OrderRecordBean data, IViewInjector injector, List<Object> list) {
                injector
                        .text(R.id.tvName, StringUtil.getHideName(data.isBuyer() ? data.getChu_name() : data.getGou_name()))
                        .text(R.id.tvOrderNum, data.getOrder_num())
                        .text(R.id.tvStatus, statusMap.get(data.getStatus()))
                        .text(R.id.tvPriceTitle, String.format(App.INSTANCE.getString(R.string.fabi_orderRecordActivity2_j), data.isBuyer() ? FabiBuySellEnum.BUY.getTitle() : FabiBuySellEnum.SELL.getTitle()))
                        .text(R.id.tvBuySellType, data.isBuyer() ?
                                FabiBuySellEnum.BUY.getTitle() :
                                FabiBuySellEnum.SELL.getTitle())
                        .textColor(R.id.tvBuySellType,
                                ContextCompat.getColor(App.INSTANCE, data.isBuyer() ?
                                        FabiBuySellEnum.BUY.getColor() :
                                        FabiBuySellEnum.SELL.getColor()))
                        .text(R.id.tvLimitMoney, String.format("%s-%s", data.getMin_price(), data.getMax_price()))
                        .text(R.id.tvPrice, CoinUtil.keepRMB(data.getPrice())+" TWD")
                        .text(R.id.tvNum, CoinUtil.keepUSDT(data.getTotal_num())+" USDT")
                        .text(R.id.tvTime, data.getAdd_time())
                        .text(R.id.tvRMB, CoinUtil.keepRMB(data.getTotal_price()) + Constans.DANWEI)
//                        .text(R.id.tv_order_date, data.getAdd_time())
                ;
            }
        });
        slimAdapter.attachTo(recyclerView).updateData(new ArrayList());
        ModelRx2DataSource<OrderRecordBean> dataSource =
                new ModelRx2DataSource<>((ModelRx2DataSource.OnLoadSource<OrderRecordBean>)
                        page -> mPresenter.getData(page + "", "20"));
        mvcHelper = new MVCCoolHelper<>(coolRefreshView);
        mvcHelper.setDataSource(dataSource);
        mvcHelper.setAdapter(slimAdapter);
        mvcHelper.refresh();
        ItemClickSupport.addTo(recyclerView)
                .setOnItemClickListener((recyclerView1, position, v) -> {
                    OrderRecordBean dataItem = (OrderRecordBean) slimAdapter.getDataItem(position);
                    boolean isBuy = dataItem.isBuyer();
                    ARouter.getInstance().build(RConfig.FABI_ORDER_RECORD_DETAIL)
                            .withString(Constans.ORDER_NUM, dataItem.getOrder_num())
                            .withBoolean(Constans.IS_BUYER, isBuy)
                            .navigation();
                });
    }
}
